#
# This file is part of the CMaNGOS Project. See AUTHORS file for Copyright information
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

include(ExternalProject)

if(WIN32)
  # create config.h and include appropriate header file
  if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/ace/config.h)
    file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/ace/config.h "#include \"ace/config-win32.h\" /* generated by CMake */\n")
  endif()
endif()

if(WIN32 AND MSVC)
  # VS100 uses MSBuild.exe instead of devenv.com, so force it to use devenv.com
  if(VS100_FOUND)
    set(ACE_BUILD_TOOL ${VS100_DIR}/devenv.com)
  else()
    set(ACE_BUILD_TOOL ${CMAKE_BUILD_TOOL})
  endif()

  if(PLATFORM MATCHES X86)
    set(ACE_CONFIGURATION Win32)
  else()
    set(ACE_CONFIGURATION x64)
  endif()

  ExternalProject_Add(ACE_Project
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}
    BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}
    INSTALL_DIR ${CMAKE_INSTALL_PREFIX}
    DOWNLOAD_COMMAND ""
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ""
    INSTALL_COMMAND ""
  )
  ExternalProject_Add_Step(ACE_Project ACE_Upgrade
    COMMAND ${ACE_BUILD_TOOL} <SOURCE_DIR>\\ace\\ace_vc8.sln /upgrade
    ALWAYS 0
  )
  ExternalProject_Add_Step(ACE_Project ACE_Build
    DEPENDEES ACE_Upgrade
    COMMAND ${ACE_BUILD_TOOL} <SOURCE_DIR>\\ace\\ace_vc8.sln /project ACE /build ${CMAKE_BUILD_TYPE}|${ACE_CONFIGURATION}
    ALWAYS 0
  )
elseif(UNIX)
  ExternalProject_Add(ACE_Project
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}
    BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}
    INSTALL_DIR ${CMAKE_INSTALL_PREFIX}
    DOWNLOAD_COMMAND ""
    CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/configure --prefix=${CMAKE_INSTALL_PREFIX} --disable-ssl
  )
else()
  message(FATAL_ERROR
    "This script doesn't support your system configuration to compile/install ACE."
  )
endif()
